Post

Replies

Boosts

Views

Activity

Reply to NSFetchedResultsController index out of bounds during context merging changes
Thank you for your reply, @DTS Engineer! Yes, the app uses NSFetchedResultsControllerDelegate to refresh the UI, and controllerDidChangeContent(_:) is the only method that is implemented throughout the app. Unfortunately, I have not yet been able to reproduce this crash myself. I noticed in the Xcode organizer that it affected 20 devices during the past two weeks and around 230 devices past year. It seems pretty rare but would of course be very nice to understand and eliminate these crashes. To my understanding it looks like Core Data is stepping out of bounds when accessing some internal data representation in NSFetchedResultsController. However, I struggle to understand how that can happen during a single merge that is all taking place on the same (main) thread. Is it be possible that the input notification to -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] could have become invalid in some edge cases at the time of actually performing the merge? Or do you have any other advice on how to reach a stage where I can reproduce / work towards a fix for this issue? :) All the best, Oscar
Jan ’25
Reply to NavigationSplitView list selection buggy after deleting?
Thank you for your reply @Claude31! Saving the context did unfortunately not help. Interestingly I could recreate the same issue using plain Swift structs as models instead of SwiftData, so it seems to be related to SwiftUI rather than SwiftData. The link that you provided led me to another Apple sample project in which I was able to find a solution: Provide the list with a selection, and Use NavigationLink with value and move detail to the NavigationSplitView I'm including these changes below in case anyone else runs into the same issue: import SwiftUI import SwiftData struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var items: [Item] @State private var selection: Item? var body: some View { NavigationSplitView { List(selection: $selection) { ForEach(items) { item in NavigationLink(value: item) { Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) } } .onDelete(perform: deleteItems) } .navigationSplitViewColumnWidth(min: 180, ideal: 200) .toolbar { ToolbarItem { Button(action: addItem) { Label("Add Item", systemImage: "plus") } } } } detail: { if let item = selection { Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))") } else { Text("Select an item") } } } private func addItem() { withAnimation { let newItem = Item(timestamp: Date()) modelContext.insert(newItem) } } private func deleteItems(offsets: IndexSet) { withAnimation { for index in offsets { modelContext.delete(items[index]) } } } }
Dec ’24
Reply to Xcode 14: Publishing changes from within view updates
I get this warning for certain view hierarchies. The following code triggers the warning, but if I move the Button out of the Form or change Form to be a VStack, the warning disappears. It seems like a bug to me, and I have filed a bug report. struct ContentView: View {     @StateObject var viewModel = ViewModel()          var body: some View {         Form {             Button("Save") {                 viewModel.counter += 1             }         }     } } class ViewModel: ObservableObject {     @Published var counter = 0 }
Oct ’22